<?php

declare(strict_types=1);

namespace App\Http\Controllers\Manage;

use App\Repositories\Manage\PrinterRepository;

class PrinterController extends ManageBaseController
{

    private $printerRepo = NULL;

    public function __construct(PrinterRepository $printerRepo)
    {
        parent::__construct();
        $this->printerRepo = $printerRepo;
    }

    public function index()
    {
        $filter = request()->all();
        $res = $this->printerRepo->getList($filter, $this->pageNum, $this->pageSize);
        return retSuc($res);
    }

    public function detail($id)
    {
        $id = intval($id);
        if (!$id)
            return retFail("参数错误");
        $res = $this->printerRepo->getOne($id);
        return retSuc($res);
    }

    /**
     * 清空待打印
     * @param $id
     */
    public function clear($id)
    {
        $id = intval($id);
        if (!$id)
            return retFail("参数错误");

        return $this->printerRepo->clearPrinter($id);
    }

    /**
     * 初始化打印机
     * @param $id
     */
    public function init($id)
    {
        $id = intval($id);
        if (!$id)
            return retFail("参数错误");

        return $this->printerRepo->initPrinter($id);
    }

    public function update($id = null)
    {
        $data = request()->only(['id', 'shop_id', 'type', 'title', 'sn', 'secret', 'status']);
        $last_id = $this->printerRepo->saveOrUpdate($data, $id ?? 0);
        if (!$last_id) {
            return retFail(empty($data['id']) ? "新增失败" : "更新失败");
        }
        $res = $this->printerRepo->getOne($last_id);
        return retSuc($res);
    }

    public function delete($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return retFail("参数错误");
        }

        return $this->printerRepo->deletePrinter($id);
    }

}
